package com.yanqu.road.dataHelper.action.zhounianqing;

import com.yanqu.road.dataHelper.action.excel.ZhouNianQingRankData;
import com.yanqu.road.dataHelper.action.excel.ZhouNianRankLogic;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class MammonDataETLCaller implements Callable<List<ZhouNianQingRankData>> {

    private List<Integer> eventType;

    private int rankType;

    ServerInfo serverInfo;

    int size;

    public MammonDataETLCaller(List<Integer> eventType, int rankType, ServerInfo serverInfo, int size) {
        this.eventType = eventType;
        this.rankType = rankType;
        this.serverInfo = serverInfo;
        this.size = size;
    }

    @Override
    public List<ZhouNianQingRankData> call() throws Exception {

        GameServer testGameServer = new GameServer(serverInfo);
        ZhouNianRankLogic resource = new ZhouNianRankLogic(testGameServer);

        //各玩家各个称号的统计
        List<MammonCount> mammonCountList = resource.getMammonCount(ConfigMgr.mammonDecorationList, "2022-08-15 00:00:00");

        List<ZhouNianQingRankData> serverRankList = new ArrayList<>();

        long userId = 0;
        ZhouNianQingRankData tempData = null;
        List<String> kv = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < mammonCountList.size(); i++) {
            MammonCount mammonCount = mammonCountList.get(i);
            if(userId != mammonCount.getUserId()){
                //更新数据
                if(tempData != null){
                    tempData.setValue(""+ count);
                    tempData.setKv(StringUtils.listToString(kv, "|"));
                }
                //初始化当前玩家数据
                tempData = new ZhouNianQingRankData(0, serverInfo.getServerId(), mammonCount.getUserId(), rankType, "");
                serverRankList.add(tempData);
                userId = mammonCount.getUserId();
                count = 0;
                kv = new ArrayList<>();
            }
            count += mammonCount.getCount();
            kv.add(mammonCount.getDecorationId() + "," + mammonCount.getCount());
            int level = ConfigMgr.decorationLevelMap.get(mammonCount.getDecorationId());
            if(!tempData.getMammonCountMap().containsKey(level)){
                tempData.getMammonCountMap().put(level, 0);
            }
            tempData.getMammonCountMap().put(level, mammonCount.getCount() + tempData.getMammonCountMap().get(level));
            if(i == mammonCountList.size() - 1){
                tempData.setValue(""+ count);
                tempData.setKv(StringUtils.listToString(kv, "|"));
            }
        }

        //补充玩家赚速
        for (ZhouNianQingRankData rankData : serverRankList) {
            UserInfo userInfo = resource.getUserInfo(rankData.getUserId());
            if(userInfo != null){
                rankData.setEarnSpeed(userInfo.getEarnSpeed());
            }
        }

        //财神数据
        //财神称号总数
        //财神1,总数|财神2,总数|
        MammonRankDataSort mammonRankDataSort = new MammonRankDataSort();
        serverRankList.sort(mammonRankDataSort);

        List<ZhouNianQingRankData> resultList = new ArrayList();
        int size = 100 > serverRankList.size() ? serverRankList.size() : 100;
        for (int i = 0; i < size; i++) {
            resultList.add(serverRankList.get(i));
        }
        return resultList;
    }
}
